#!/bin/python
# -*- coding:utf-8 -*-


import sys, time

class ProgressBar:

	def __init__(self, count = 0, total = 0, width = 50):
		self.count = count
		self.total = total
		self.width = width

	def move(self,count):
		if count is not None:
			self.count = count
		else:
			self.count += 1

	def show_progress_move(self, s="Current Progress", count=None):
		self.move(count)

		progress = int(self.count * self.width / self.total)
		percent = self.count * 100.0 / self.total
		sys.stdout.write(' ' * (self.width + 9) + '\r')
		sys.stdout.flush()
		print('')
		sys.stdout.write('{0}: {1:3} of {2:3}: '.format(s, self.count, self.total))
		sys.stdout.write('[' + '>' * int(progress) + '-' * (self.width - int(progress)) + ']' + '%.2f'%percent + '%' + '\r')
		if progress == self.width:
			sys.stdout.write('\n')
		sys.stdout.flush()

	def show_progress_static(self, count=None):
		self.move(count)
		num_arrow = int(self.count * self.width / self.total) #计算显示多少个'>'
		num_line = self.width - num_arrow #计算显示多少个'-'
		sys.stdout.write('{0:3} of {1:3}: '.format(self.count, self.total))
		percent = self.count * 100.0 / self.total #计算完成进度，格式为xx.xx%
		process_bar = '[' + '>' * num_arrow + '-' * num_line + ']'\
					  + '%.2f' % percent + '%' + '\r' #带输出的字符串，'\r'表示不换行回到最左边
		sys.stdout.write(process_bar) #这两句打印字符到终端
		sys.stdout.flush()

	def close(self, words='done'):
		print ('')
		print (words)
		self.count = 0

if __name__ == '__main__':
	bar = ProgressBar(total = 10)
	for i in range(10):
		bar.show_progress_move("Total")
		# bar.show_progress_static()
		time.sleep(1)